package cn.ac.cintcm.synonymous;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

public class BookMark implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -7942206695112500425L;
    private String author;
    private String id;
    private Date date;
    private static HashMap<String, BookMark> bookmarks = new HashMap<String, BookMark>();
    
    public BookMark() {
        
    }
    public BookMark(String author, String id) {
        this.author = author;
        this.id = id;
        this.date = new Date();
    }
    
    public BookMark(String author, String id, String date) {
        this.author = author;
        this.id = id;
        try {
            this.date = Config.sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    public BookMark(String line) {
        if (line != null) {
            String a[] = line.split(Config.FIELD_SEPERATOR);
            if (a.length == 3) {
                this.author = a[0];
                this.id = a[1];
                try {
                    if (a[2] != null) {
                        this.date = Config.sdf.parse(a[2]);
                    } else {
                        this.date = new Date();
                    }
                } catch (ParseException e) {
                }
            }
            
        }
    }   
    
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    
    public static void read() {
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(Config.getConfigDir() + Config.BOOK_MARK));
            String line = null;
            while((line = reader.readLine()) != null) {
                BookMark b = new BookMark(line);
                bookmarks.put(b.getAuthor(), b);
            }
            reader.close();
        } catch (IOException e) {
        }
    }
    
    public static void write() {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(Config.getConfigDir() + Config.BOOK_MARK));
            for (BookMark b : bookmarks.values()) {
                writer.write(b.toString());
                writer.write(Config.ROW_SEPERATOR);
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(author);
        sb.append(Config.FIELD_SEPERATOR);
        sb.append(id);
        sb.append(Config.FIELD_SEPERATOR);
        sb.append(date);
        return sb.toString();
        
    }
    
    public static BookMark getBookMark(String author) {
        return bookmarks.get(author);
    }
    
    public static  void addBookMark(String author, BookMark bm) {
        bookmarks.put(author, bm);
    }
    
    public static void deleteBookMark(String author) {
        bookmarks.remove(author);
    }
}
